# container.py
import wx
import logging, ctypes
from pysage import ActorManager, Actor
import subprocess

omanager = ActorManager.get_singleton()

class Container(wx.Panel, Actor):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        Actor.__init__(self)
        self.process = None   
        
    def onDirectoryChange(self, evt):
        _path = evt.GetString()
        return True
    
    def __del__(self):
        '''need to make sure that the subprocess is killed before the main gui exits'''
        pass

    def OnExecuteBtn(self, evt):
        cmd = self.cmd.GetValue()
        self.process = subprocess.Popen(cmd)
        logging.debug('started process pid: %s' % self.process.pid)
        self.startBtn.Disable()
        self.endBtn.Enable()
    def OnEndBtn(self, evt):
        ctypes.windll.kernel32.TerminateProcess(int(self.process._handle), -1)
        # os.popen('TASKKILL /PID '+str(self.process.pid)+' /F')
        self.process.wait()
        logging.debug('process.poll(): %s' % self.process.poll())
        self.startBtn.Enable()
        self.endBtn.Disable()
        
        